package com.rmfyzxfw.caseprocess.model.dynamic.condition;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.rmfyzxfw.caseprocess.enums.SearchTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author long@flyfox
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ConditionSearch<T> implements DynamicCondition {

    private String field;
    private Object keyword;
    /**
     * 搜索类型 默认精确
     */
    private SearchTypeEnum searchTypeEnum;


    public ConditionSearch(String field, Object keyword) {
        this.field = field;
        this.keyword = keyword;
    }

    public String getSearchType() {
        if (searchTypeEnum == null) {
            return SearchTypeEnum.EXACT.name();
        }
        return searchTypeEnum.name();
    }

    @Override
    public String generateSqlCondition() {
        if (ObjectUtils.isNotEmpty(keyword)) {
            if (searchTypeEnum == SearchTypeEnum.FUZZY) {
                return field + " LIKE CONCAT('%', '" + keyword.toString() + "', '%')";
            } else {
                return field + " = '" + keyword.toString() + "'";
            }
        }
        return null;
    }
}

